<?php

namespace component\element\aieditor\controller;

use core\component\ElementBase;
use core\lib\upload\UploadImage;
use core\traits\ElementView as TraitsElementView;
use Exception;
use Noodlehaus\Config;
use support\View;

class Index extends ElementBase
{
    public function index(...$param)
    {
        $editorconfig = Config::load(get_element_path() . '/aieditor/config.ini');
        View::assign('editorconfig', $editorconfig['value']);
        return $this->view($param);
    }


    public function upload()
    {
        try {

            $plugin = request()->elementinfo['plugin'] == '' ? 'frame'
                : request()->elementinfo['plugin'];

            $uploadImage = new UploadImage(
                path: $plugin,
            );
            $res = $uploadImage->upload();
            return json([
                'errorCode' => 0,
                'data' => [
                    'src' => $res['path'],
                ]
            ]);
        } catch (Exception $ex) {
            return json([
                'errorCode' => -1,
            ]);
        }
    }
}
